﻿using System;
using System.Collections.Generic;

using DataFactory.Cms;
using IDAL.Cms;
using Model.Cms;
using Model.Config;
using Model.AuthModel;
using Model.Base;
namespace BLL.Cms
{
    /// <summary>
    /// 业务层，内容管理，新闻
    /// </summary>
    public class NewsBLL
    {
        /// <summary>
        ///  创建新闻映射对象
        /// </summary>
        private INews dal = DataAccess.CreateNews();

        /// <summary>
        /// 新增新闻
        /// </summary>
        /// <param name="newsType"></param>
        /// <param name="newsTitle"></param>
        /// <param name="filepath"></param>
        /// <param name="newsSummary"></param>
        /// <param name="newsContent"></param>
        /// <param name="newsLink"></param>
        /// <param name="recommend"></param>
        /// <param name="staff"></param>
        /// <param name="isonline"></param>
        /// <param name="onlineTime"></param>
        /// <returns></returns>
        public bool InsertNews(int newsType, string newsTitle, string filepath, string newsSummary, string newsContent
            , string newsLink, string recommend, string author, StaffInfo staff, string isonline, DateTime onlineTime) 
        {
            NewsInfo info = new NewsInfo();
            info.NewsType = new TermInfo();
            info.NewsType.TermID = newsType;
            info.NewsTitle = newsTitle;
            info.Filepath = filepath;
            info.NewsSummary = newsSummary;
            info.NewsContent = newsContent;
            info.NewsLink = newsLink;
            info.Recommend = recommend;
            info.Author = author;
            info.Staff = staff;
            info.IsOnline = isonline;
            info.OnlineTime = onlineTime;
            return dal.InsertNews(info);
        }

        public bool UpdateNews(int newsID, int newsType, string newsTitle, string filepath, string newsSummary, string newsContent
            , string newsLink, string recommend, string author, StaffInfo staff, string isonline, DateTime onlineTime)
        {
            NewsInfo info = new NewsInfo();
            info.NewsID = newsID;
            info.NewsType = new TermInfo();
            info.NewsType.TermID = newsType;
            info.NewsTitle = newsTitle;
            info.Filepath = filepath;
            info.NewsSummary = newsSummary;
            info.NewsContent = newsContent;
            info.NewsLink = newsLink;
            info.Recommend = recommend;
            info.Author = author;
            info.Staff = staff;
            info.IsOnline = isonline;
            info.OnlineTime = onlineTime;
            return dal.UpdateNews(info);
        }

        public NewsInfo GetNewsByID(int id) 
        {
            return dal.GetNewsByID(id);
        }

        /// <summary>
        /// 前台根据父级新闻列表
        /// </summary>
        /// <param name="newsType"></param>
        /// <param name="top"></param>
        /// <returns></returns>
        public IList<NewsInfo> FrontListByParentTerm(int newsType, int top)
        {
            NewsCondition condition = new NewsCondition();
            condition.ParentTermID = newsType;
            condition.IsOnline = "y";
            condition.TimeType = ListTime.Online;
            condition.Top = top;
            condition.OrderBy = "AddTime DESC";
            return dal.ListNews(condition);
        }

        /// <summary>
        /// 前台新闻列表
        /// </summary>
        /// <param name="newsType"></param>
        /// <param name="top"></param>
        /// <returns></returns>
        public IList<NewsInfo> FrontListByTerm(int newsType, int top) 
        {
            NewsCondition condition = new NewsCondition();
            condition.NewsTypeID = newsType;
            condition.IsOnline = "y";
            condition.TimeType = ListTime.Online;
            condition.Top = top;
            condition.OrderBy = "OnlineTime DESC";
            return dal.ListNews(condition);
        }

        public IList<NewsInfo> FrontListByTerm(int newsType, int pageIndex, int pageSize, out int recordCount)
        {
            NewsCondition condition = new NewsCondition();
            condition.NewsTypeID = newsType;
            condition.IsOnline = "y";
            condition.TimeType = ListTime.Online;
            condition.PageIndex = pageIndex;
            condition.PageSize = pageSize;
            condition.OrderBy = "OnlineTime DESC";
            return dal.ListNews(condition, out recordCount);
        }

        /// <summary>
        /// 后台新闻列表
        /// </summary>
        /// <param name="newsType"></param>
        /// <param name="year"></param>
        /// <param name="isonline"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IList<NewsInfo> BackList(int newsType, string year, string isonline, int pageIndex, int pageSize, out int recordCount) 
        {
            NewsCondition condition = new NewsCondition();
            condition.NewsTypeID = newsType;
            condition.Year = year;
            condition.IsOnline = isonline;
            condition.PageIndex = pageIndex;
            condition.PageSize = pageSize;
            condition.OrderBy = "AddTime DESC ";
            return dal.ListNews(condition, out recordCount);
        }
    }
}
